#!/bin/bash
# Author: Jan Drabek

echo "Deploy script";
echo "----------------------------------------------------------";
server="ctenari.cz";
user="devel";
password=""
project="http://preader.googlecode.com/svn/trunk/"

# Test if variables are filled
if [[ $server == "" ]] || [[ $user == "" ]] || [[ $password  == "" ]] || [[ $project == "" ]]; then
	echo "Main variables are not set, please edit this script";
	exit 1;
fi

# Test avability of needed programs
type -P ftpsync &>/dev/null || { echo "Program ftpsync needed (http://ossw.ibcl.at/FTPSync/). Aborting." >&2; exit 1;}
type -P svn &>/dev/null || { echo "Subversion needed. Aborting." >&2; exit 1;}
type -P find &>/dev/null || { echo "Program find needed. Aborting." >&2; exit 1;}

# Print help if wanted
if [ "$1" ==  "--help" ]; then
	echo "Update version from svn to devel ftp.";
	echo "		(without paramer) run deploy";
	echo "--ignore-externals	ignore all external dependencies when update"
	echo "--checkout	delete all files, and do new checkout";
	echo "--help		show this help";
	exit;
fi

# Start
echo "Starting PREADER deploy...";
if [ ! -d "./preader" ]; then
	mkdir ./preader
fi

# New checkout
if [ "$1" == "--checkout" ]; then
	# Remove remote copy
	echo "Please remove all files from FTP";
	read -p "Enter this after it is done"
	# Remove local copy
	rm -Rfv	./preader/*
	rm -Rfv "./preader/.svn"
	cd ./preader
	# Checkout
	echo "Doing checkout";
	svn checkout $project .
else

# Update svn
	echo "Updating working copy...";
	cd ./preader/
	if [ "$1" == "--ignore-externals" ]; then
		svn update --ignore-externals
	else
		svn update
	fi
fi

# Copy changed to ftp
echo "Syncing to remote site...";
ftpsync -S . ftp://$user:$password@$server// ignoremask=.svn
echo "Syncing done in $TOTAL minute(s)!";